package com.java.myapp;

import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;

public class Plogin extends JFrame {

	public static String userName;
	public static String passWord;
	static JLabel lblWelcome;
	private JTextField textField;
	private JTextField textField_1;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				Plogin form = new Plogin();
				form.setVisible(true);
			}
		});
	}
	
	
	public Plogin() {
            
		// Create Form Frame
		super("CalMe");
		setSize(679, 385);
		setLocation(500, 280);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
		
		
		JLabel lblUsername = new JLabel("Username :");
		lblUsername.setBounds(359, 74, 84, 24);
		getContentPane().add(lblUsername);
		
		textField = new JTextField();
		textField.setBounds(426, 76, 86, 20);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password :");
		lblPassword.setBounds(359, 112, 72, 20);
		getContentPane().add(lblPassword);
		
		textField_1 = new JTextField();
		textField_1.setBounds(426, 109, 86, 20);
		getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnOk = new JButton("OK");
		btnOk.setBounds(397, 190, 89, 23);
		getContentPane().add(btnOk);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.setBounds(397, 156, 89, 23);
		getContentPane().add(btnRegister);
		
		/* Label Welcome
		lblWelcome = new JLabel("lblWelcome",JLabel.CENTER);
		lblWelcome.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblWelcome.setBounds(163, 154, 336, 25);
		getContentPane().add(lblWelcome);*/
		
		//When Frame Loaded
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				LoginDialog();
			}
		});
					
	}
	
	public static Boolean getLogin() {
		
		Connection connect = null;
		PreparedStatement pre = null;
		Boolean status = false;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");

			connect =  DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
					"?user=root&password=root");	
			
			String sql = " SELECT * FROM  member " +
					" WHERE Username = ? " +
					" AND Password = ? ";
			pre = connect.prepareStatement(sql);
			pre.setString(1, userName);
			pre.setString(2, passWord);

			ResultSet rec = pre.executeQuery();
			/*if(rec.next()) {
				lblWelcome.setText("Welcome : " + rec.getString("userName"));
				status = true;
			} else {
     		JOptionPane.showMessageDialog(null, "Incorrect Username/Password");
			}*/
             
		} catch (Exception e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, e.getMessage());
			e.printStackTrace();
		}
		
		try {
			if(pre != null) {
				pre.close();
				connect.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		return status;
	}
	
	public static void LoginDialog() {

		JLabel title = new JLabel("Login Username and Password");
		JTextField username = new JTextField();
		JPasswordField password = new JPasswordField();
		final JComponent[] inputs = new JComponent[] {
				title,
				new JLabel("Username"),
				username,
				new JLabel("Password"),
				password
		};
		/*JOptionPane.showMessageDialog(null, inputs, "Login", JOptionPane.PLAIN_MESSAGE);
		
		userName = username.getText();
		passWord =  new String(password.getPassword()); */
		
		// Check Login
		if(!getLogin())
		{
			LoginDialog();
		}
		
	}
}
